//Dijkstra
#include <climits>
#include <cstdio>

int map[21][21];
bool mark[21];
int distance[21];
int dijkstra(int start, int end);

int main(int argc, char const* argv[])
{
    int num;
    int set_count = 0;
    while (scanf("%d", &num) != EOF) {
        for (int i = 0; i < 21; i++) {
            for (int j = 0; j < 21; j++) {
                map[i][j] = 0;
            }
        }
        int id;
        for (int j = 0; j < num; j++) {
            scanf("%d", &id);
            map[1][id] = 1;
            map[id][1] = 1;
        }
        for (int i = 2; i <= 19; i++) {
            scanf("%d", &num);
            for (int j = 0; j < num; j++) {
                scanf("%d", &id);
                map[i][id] = 1;
                map[id][i] = 1;
            }
        }
        printf("Test Set #%d\n", ++set_count);
        int case_num;
        scanf("%d", &case_num);
        for (int i = 0; i < case_num; i++) {
            int start, end;
            scanf("%d%d", &start, &end);
            printf("%d to %d: %d\n", start, end, dijkstra(start, end));
        }
        printf("\n");
    }
    return 0;
}

int dijkstra(int start, int end)
{
    for (int i = 0; i < 21; i++) {
        mark[i] = false;
        distance[i] = -1;
    }
    distance[start] = 0;
    int new_node = start;
    mark[start] = true;
    //遍历n-1次
    for (int i = 0; i < 20; i++) {
        for (int j = 1; j <= 20; j++) {
            if (mark[j])
                continue;
            if (map[new_node][j] == 0)
                continue;
            if (distance[j] == -1 || distance[j] > distance[new_node] + map[new_node][j])
                distance[j] = distance[new_node] + map[new_node][j];
        }
        int min = INT_MAX;
        for (int j = 1; j <= 20; j++) {
            if (mark[j])
                continue;
            if (distance[j] == -1)
                continue;
            if (distance[j] < min) {
                min = distance[j];
                new_node = j;
            }
        }
        mark[new_node] = true;
    }
    return distance[end];
}